//
//  MainViewController.swift
//  Jock
//
//  Created by HD on 15/3/11.
//  Copyright (c) 2015年 Haidy. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController, TabViewDelegate {
    
    private var tabBarView: TabView?
    private var titles = ["糗事", "发现", "纸条", "我"]

    override func viewDidLoad() {
        super.viewDidLoad()
        initView()
        initController()
    }
    
    // 初始化界面
    private func initView() {
        self.title = "最新"
        self.navigationController?.navigationBarHidden = false
        self.navigationItem.hidesBackButton = true
//        self.automaticallyAdjustsScrollViewInsets = false
        self.view.backgroundColor = UIColor.whiteColor()
        self.tabBar.hidden = true
        
        initTabBar()
    }
    
    private func initTabBar() {
        let width = self.view.frame.width
        tabBarView = TabView(frame: CGRectMake(0, self.view.frame.height - 48, width, 48))
        tabBarView?.backgroundColor = UIColor(red: 251 / 255.0, green: 173 / 255.0, blue: 69 / 255.0, alpha: 1)
        tabBarView?.tabViewDelegate = self
        //糗事
        var jock = ViewUtil.instanceView("TabItem") as! TabItem
        jock.setImage(UIImage(named: "jock_icon_small_normal.png")!, selectedImage: UIImage(named: "jock_icon_small_selected.png")!)
        jock.select = true
        jock.title = titles[0]
        let found = ViewUtil.instanceView("TabItem") as! TabItem
        found.title = titles[1]
        found.setImage(UIImage(named: "found_icon_normal.png")!, selectedImage: UIImage(named: "found_icon_selected.png")!)
        let message = ViewUtil.instanceView("TabItem") as! TabItem
        message.title = titles[2]
        message.setImage(UIImage(named: "message_icon_normal.png")!, selectedImage: UIImage(named: "message_icon_selected.png")!)
        let me = ViewUtil.instanceView("TabItem") as! TabItem
        me.title = titles[3]
        me.setImage(UIImage(named: "me_icon_normal.png")!, selectedImage: UIImage(named: "me_icon_selected.png")!)
        self.tabBarView?.setTabItems([jock, found, message, me])
        self.view.addSubview(tabBarView!)
    }
    
    func select(item: TabItem, index: Int) {
        self.selectedIndex = index
        self.title = titles[index]
    }
    
    private func initController() {
        var vc1 = NewsViewController()
        var vc2 = FoundViewController()
        var vc3 = TruthViewController()
        var vc4 = MeViewController()
        self.viewControllers = [vc1, vc2, vc3, vc4]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
